package com.github.yangyishe.p200.p117;

import com.github.yangyishe.TreeNode;

public class Node {
    public int val;
    public Node left;
    public Node right;
    public Node next;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, Node _left, Node _right, Node _next) {
        val = _val;
        left = _left;
        right = _right;
        next = _next;
    }

    public static Node getInstance(Integer[] arr){
        Node[] treeNodes=new Node[arr.length];
        for(int i=0;i<arr.length;i++){
            if(arr[i]!=null){
                treeNodes[i]=new Node(arr[i]);
            }
        }

        for(int i=0;i<=treeNodes.length/2-1;i++){
            if(treeNodes[i]!=null&&i*2+1<treeNodes.length){
                treeNodes[i].left=treeNodes[i*2+1];
            }
            if(treeNodes[i]!=null&&i*2+2<treeNodes.length){
                treeNodes[i].right=treeNodes[i*2+2];
            }
        }


        return treeNodes[0];
    }

    public void preOrder(){
        System.out.println(this);
        if(this.left!=null){
            this.left.preOrder();
        }
        if(this.right!=null){
            this.right.preOrder();
        }
    }

    @Override
    public String toString() {
        return "Node{" +
                "val=" + val +
                '}';
    }
}
